import { createRouter, createWebHashHistory, Router } from "vue-router"
import { checkLogin } from "./checkLogin"
import { getJsUrl } from "@ui/utils/url/getJsUrl"
import { firstUpper } from "@ui/utils/win/firstUpper"
export const xtlj = ['xtm', 'mk', 'zj']
export let routerTo: any
function getHome(len: number) {
  var path = '/home', name = 'home', xtm = xtlj.slice(0, len)
  xtm.forEach(it => {
    path += '/:' + it
    name += firstUpper(it, true)
  })
  if (len) {
    path += '/:page'
  }
  return {
    path,
    name,
    component: () => import('@ui/views/home/index.vue')
  }
}
export const routes = [
  {
    path: '/', meta: { requiresLogin: true }, redirect: '/home'
  }
  , ...xtlj.map((_it, i) => getHome(i))
  , getHome(xtlj.length)
  , {
    path: '/login',
    meta: { requiresLogin: true },
    name: 'login',
    component: () => import('@ui/views/login/index.vue')
  }
], router: Router = createRouter({
  history: createWebHashHistory(getJsUrl()),
  routes
})
router.beforeEach(async (to, from) => {
  const judge = await checkLogin(to)
  if (!judge && to.path != '/login') {
    routerTo = to
    return { path: '/login', replace: true }
  }
  return true
})